package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryDutyHistoryDTO;
import com.neusoft.neuiotms.dcam.fixed.model.InventoryDutyHistory;
import com.neusoft.neuiotms.dcam.fixed.repository.InventoryDutyHistoryRepository;

@Component
public class InventoryDutyHistoryConverter extends AbstractConverter<InventoryDutyHistory, InventoryDutyHistoryDTO> {

    @Autowired
    private InventoryDutyHistoryRepository repository;

    @Override
    public InventoryDutyHistory toModel(final InventoryDutyHistoryDTO dto) {
        InventoryDutyHistory model;

        if (dto.isNew()) {
            model = new InventoryDutyHistory();
        } else {
            model = this.repository.getOne(dto.getId());
        }
        model.setDutyCode(dto.getDutyCode());
        model.setComment(dto.getComment());
        model.setStatus(dto.getStatus());
        model.setCreateTime(Calendar.getInstance().getTime());

        return model;
    }

    @Override
    public InventoryDutyHistoryDTO toDTO(final InventoryDutyHistory model, final boolean forListView) {

        final InventoryDutyHistoryDTO dto = new InventoryDutyHistoryDTO();

        dto.setId(model.getId());
        dto.setDutyCode(model.getDutyCode());
        dto.setComment(model.getComment());
        dto.setStatus(model.getStatus());
        dto.setCreateTime(model.getCreateTime());

        return dto;
    }

}
